iT邦幫忙

2025 iThome 鐵人賽

DAY 25
0
Software Development

clojure 30 days系列 第 25

clojure 30 days - day 23

  • 分享至 

  • xImage
  •  

Problem Description

Definition
Strong number is the number that the sum of the factorial of its digits is equal to number itself.

For example, 145 is strong, since 1! + 4! + 5! = 1 + 24 + 120 = 145.

Task
Given a number, Find if it is Strong or not and return either "STRONG!!!!" or "Not Strong !!".

Implementation

; implement
(defn strong [n]
  (let [factorial (fn [x]
                    (if (<= x 1)
                      1
                      (apply * (range 1 (inc x)))))
        factorial-sum (->> n
                           str
                           (map #(Integer/parseInt (str %)))
                           (map factorial)
                           (apply +))]
    (if (= n factorial-sum)
      "STRONG!!!!"
      "Not Strong !!")))

; test
; execute implement function
(defn tester [arg exp]
  (= (strong arg) exp))

; args & exception
(comment
  (tester 1 "STRONG!!!!")
  (tester 2 "STRONG!!!!")
  (tester 145 "STRONG!!!!")
  (tester 40585 "STRONG!!!!")
  (tester 7 "Not Strong !!")
  (tester 93 "Not Strong !!")
  (tester 185 "Not Strong !!"))


上一篇
clojure 30 days - day 22
系列文
clojure 30 days25
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言